<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="google-site-verification" content="xBT4GhYoi5qRD5tr338pgPM5OWHHIDR6mNg1a3euekI" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="keyword"  content="Tooi">
    <link rel="shortcut icon" href="/img/favicon.ico">

    <title>
        
        Docker 容器技术 &amp; Docker Compose - Tooi的博客 | Tooi-Blog
        
    </title>

    <!-- Custom CSS -->
    <link rel="stylesheet" href="/css/aircloud.css">
    <link rel="stylesheet" href="/css/gitment.css">
    <!--<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">-->
    <link href="//at.alicdn.com/t/font_620856_pl6z7sid89qkt9.css" rel="stylesheet" type="text/css">
    <!-- ga & ba script hoook -->
    <script></script>
</head>

<body>

<div class="site-nav-toggle" id="site-nav-toggle">
    <button>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
    </button>
</div>

<div class="index-about">
    <i> 瞄准月亮。 </i>
</div>

<div class="index-container">
    
    <div class="index-left">
        
<div class="nav" id="nav">
    <div class="avatar-name">
        <div class="avatar ">
            <img src="/img/avatar.jpg" />
        </div>
        <div class="name">
            <i>Tooi6</i>
        </div>
    </div>
    <div class="contents" id="nav-content">
        <ul>
            <li >
                <a href="/">
                    <i class="iconfont icon-shouye1"></i>
                    <span>主页</span>
                </a>
            </li>
            <li >
                <a href="/tags">
                    <i class="iconfont icon-biaoqian1"></i>
                    <span>标签</span>
                </a>
            </li>
            <li >
                <a href="/archives">
                    <i class="iconfont icon-guidang2"></i>
                    <span>存档</span>
                </a>
            </li>
            <li >
                <a href="/about/">
                    <i class="iconfont icon-guanyu2"></i>
                    <span>关于</span>
                </a>
            </li>
            
            <li>
                <a id="search">
                    <i class="iconfont icon-sousuo1"></i>
                    <span>搜索</span>
                </a>
            </li>
            
        </ul>
    </div>
    
        <div id="toc" class="toc-article">
    <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#简介"><span class="toc-text">简介</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#什么是Docker？"><span class="toc-text">什么是Docker？</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#虚拟机和容器的区别？"><span class="toc-text">虚拟机和容器的区别？</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#容器技术的优势"><span class="toc-text">容器技术的优势</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Docker-的基本概念"><span class="toc-text">Docker 的基本概念</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#如何使用？"><span class="toc-text">如何使用？</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#安装Docker"><span class="toc-text">安装Docker</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#配置镜像加速"><span class="toc-text">配置镜像加速</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#基本操作"><span class="toc-text">基本操作</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#使用-Dockerfile-定制镜像"><span class="toc-text">使用 Dockerfile 定制镜像</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#demo："><span class="toc-text">demo：</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#镜像构建上下文（Context）"><span class="toc-text">镜像构建上下文（Context）</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Dockerfile-指令"><span class="toc-text">Dockerfile 指令</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#使用数据卷构建MySQL"><span class="toc-text">使用数据卷构建MySQL</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#什么是数据卷？"><span class="toc-text">什么是数据卷？</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#构建MySQL"><span class="toc-text">构建MySQL</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Docker-Compose"><span class="toc-text">Docker Compose</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#什么是-Docker-Compose"><span class="toc-text">什么是 Docker Compose</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#下载、安装"><span class="toc-text">下载、安装</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#DEMO"><span class="toc-text">DEMO</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Docker-常用命令"><span class="toc-text">Docker 常用命令</span></a></li></ol>
</div>
    
</div>


<div class="search-field" id="search-field">
    <div class="search-container">
        <div class="search-input">
            <span id="esc-search"> <i class="icon-fanhui iconfont"></i></span>
            <input id="search-input"/>
            <span id="begin-search">搜索</span>
        </div>
        <div class="search-result-container" id="search-result-container">

        </div>
    </div>
</div>

        <div class="index-about-mobile">
            <i> 瞄准月亮。 </i>
        </div>
    </div>
    
    <div class="index-middle">
        <!-- Main Content -->
        


<div class="post-container">
    <div class="post-title">
        Docker 容器技术 &amp; Docker Compose
    </div>

    <div class="post-meta">
        <span class="attr">发布于：<span>2019-11-03 21:03:02</span></span>
        
        <span class="attr">标签：/
        
        <a class="tag" href="/tags/#Docker" title="Docker">Docker</a>
        <span>/</span>
        
        <a class="tag" href="/tags/#Docker Compose" title="Docker Compose">Docker Compose</a>
        <span>/</span>
        
        
        </span>
        <span class="attr">访问：<span id="busuanzi_value_page_pv"></span>
</span>
</span>
    </div>
    <div class="post-content ">
        <h3 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h3><h4 id="什么是Docker？"><a href="#什么是Docker？" class="headerlink" title="什么是Docker？"></a>什么是Docker？</h4><blockquote>
<p>Docker 是一个开源的<strong>应用容器引擎</strong>，让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中，然后发布到任何流行的 Linux或Windows 机器上，也可以实现<strong>虚拟化</strong>。容器是完全使用<strong>沙箱机制</strong>，相互之间不会有任何接口。  </p>
</blockquote>
<h4 id="虚拟机和容器的区别？"><a href="#虚拟机和容器的区别？" class="headerlink" title="虚拟机和容器的区别？"></a>虚拟机和容器的区别？</h4><blockquote>
<p><strong>虚拟机：</strong> 传统的虚拟机需要模拟整台机器包括硬件，每台虚拟机都需要有<strong>自己的操作系统</strong>，虚拟机一旦被开启，<strong>预分配给它的资源将全部被占用</strong>  </p>
</blockquote>
<blockquote>
<p><strong>容器：</strong> 容器技术是和我们的宿主机<strong>共享硬件资源及操作系统</strong>，可以实现<strong>资源的动态分配</strong>。容器包含应用和其所有的依赖包，但是与其他容器共享内核。容器在宿主机操作系统中，在用户空间以<strong>分离的进程运行。</strong>    </p>
</blockquote>
<h4 id="容器技术的优势"><a href="#容器技术的优势" class="headerlink" title="容器技术的优势"></a>容器技术的优势</h4><table>
<thead>
<tr>
<th>特性</th>
<th>容器</th>
<th>虚拟机</th>
</tr>
</thead>
<tbody><tr>
<td>启动</td>
<td>秒级</td>
<td>分钟级</td>
</tr>
<tr>
<td>硬盘使用</td>
<td>一般为MB</td>
<td>一般为GB</td>
</tr>
<tr>
<td>性能</td>
<td>接近原生</td>
<td>弱于</td>
</tr>
<tr>
<td>系统支持量</td>
<td>单机支持上千个容器</td>
<td>一般是几十个</td>
</tr>
</tbody></table>
<h3 id="Docker-的基本概念"><a href="#Docker-的基本概念" class="headerlink" title="Docker 的基本概念"></a>Docker 的基本概念</h3><ul>
<li><strong>镜像（Image）：</strong> <blockquote>
<p>Docker 镜像是一个<strong>特殊的文件系统</strong>，除了提供容器运行时所需的程序、库、资源、配置等文件外，还包含了一些为运行时准备的一些配置参数（如匿名卷、环境变量、用户等）。<strong>镜像不包含任何动态数据，其内容在构建之后也不会被改变</strong></p>
</blockquote>
</li>
<li><strong>容器（Container）：</strong> <blockquote>
<p>容器的实质是<strong>进程</strong>，但与直接在宿主执行的进程不同，容器进程运行于属于自己的独立的 命名空间。因此容器可以拥有自己的 root 文件系统、自己的网络配置、自己的进程空间，甚至自己的用户 ID 空间。容器内的进程是运行在一个<strong>隔离的环境</strong>里，使用起来，就好像是在一个独立于宿主的系统下操作一样  </p>
</blockquote>
</li>
</ul>
<p><strong>镜像（Image）和容器（Container）的关系，就像是面向对象程序设计中的 类 和 实例 一样，镜像是静态的定义，容器是镜像运行时的实体。容器可以被创建、启动、停止、删除、暂停等。</strong></p>
<ul>
<li><strong>仓库（Repository）：</strong>  </li>
</ul>
<blockquote>
<p>Docker 仓库是<strong>集中存放镜像文件的场所</strong>。镜像构建完成后，可以很容易的在当前宿主上运行，但是， 如果需要在其它服务器上使用这个镜像，我们就需要一个集中的存储、分发镜像的服务，Docker Registry (仓库注册服务器)就是这样的服务<br> 官方仓库：<a href="https://hub.docker.com/search?q=&amp;type=image" target="_blank" rel="noopener">https://hub.docker.com/search?q=&amp;type=image</a>   </p>
</blockquote>
<ul>
<li><strong>Docker引擎</strong>  <blockquote>
<p>Docker 引擎是一个包含以下主要组件的客户端服务器应用程序（CS架构）。  </p>
</blockquote>
</li>
</ul>
<p><strong>服务器(server)</strong>，它是一种称为<strong>守护进程</strong>并且长时间(后台)运行的程序。<br><strong>REST API</strong>，用于指定程序可以用来与守护进程通信的接口，并指示它做什么。<br><strong>客户端（Client）</strong>，有命令行界面 (CLI) 工具  </p>
<p><img src="https://note.youdao.com/yws/api/personal/file/851252360E394E6A8A63E181ED3E3203?method=download&shareKey=c1c71de21d8043aaa3d033697078fb02" alt="image"></p>
<h3 id="如何使用？"><a href="#如何使用？" class="headerlink" title="如何使用？"></a>如何使用？</h3><h4 id="安装Docker"><a href="#安装Docker" class="headerlink" title="安装Docker"></a>安装Docker</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"># 卸载旧版本  </span><br><span class="line">sudo apt-get autoremove docker docker-engine docker.io</span><br><span class="line"></span><br><span class="line"># 使用APT安装  </span><br><span class="line">sudo apt-get update </span><br><span class="line">## 安装必要的工具</span><br><span class="line">sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common  </span><br><span class="line">## 安装 GPG 证书</span><br><span class="line">curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -  </span><br><span class="line">## 写入软件源信息  </span><br><span class="line">sudo add-apt-repository &quot;deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable&quot;  </span><br><span class="line">## 更新并安装 </span><br><span class="line">sudo apt-get -y update</span><br><span class="line">sudo apt-get -y install docker-ce</span><br><span class="line"></span><br><span class="line"># 使用脚本自动安装（推荐）  </span><br><span class="line">curl -fsSL get.docker.com -o get-docker.sh</span><br><span class="line">sudo sh get-docker.sh --mirror Aliyun</span><br><span class="line"></span><br><span class="line"># 启动 Docker CE</span><br><span class="line">sudo systemctl enable docker</span><br><span class="line">sudo systemctl start docker</span><br><span class="line"></span><br><span class="line"># 添加docker用户组  </span><br><span class="line">groupadd docker    # 一般脚本已经建好了</span><br><span class="line">usermod -aG docker $USER</span><br><span class="line"></span><br><span class="line"># 运行hello-world</span><br><span class="line">docker run hello-world</span><br></pre></td></tr></table></figure>

<h4 id="配置镜像加速"><a href="#配置镜像加速" class="headerlink" title="配置镜像加速"></a>配置镜像加速</h4><blockquote>
<p>下面是docker官方提供的，使用阿里云的更快：<a href="https://cr.console.aliyun.com/#/accelerator" target="_blank" rel="noopener">https://cr.console.aliyun.com/#/accelerator</a></p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">vim /etc/docker/daemon.json  # 没有则新建</span><br><span class="line">## 写入内容</span><br><span class="line">&#123;</span><br><span class="line">  &quot;registry-mirrors&quot;: [</span><br><span class="line">    &quot;https://registry.docker-cn.com&quot;</span><br><span class="line">  ]</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"># 重启docker</span><br><span class="line">systemctl daemon-reload</span><br><span class="line">systemctl restart docker</span><br></pre></td></tr></table></figure>

<h4 id="基本操作"><a href="#基本操作" class="headerlink" title="基本操作"></a>基本操作</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"># 下载镜像  </span><br><span class="line">docker pull [选项] [Docker Registry 地址[:端口号]/]仓库名[:标签] </span><br><span class="line">#下载tomcat镜像，默认下载官方的latest标签</span><br><span class="line">docker pull tomcat   </span><br><span class="line"></span><br><span class="line"># 查看本地镜像  </span><br><span class="line">docker images</span><br><span class="line"></span><br><span class="line"># 运行镜像  </span><br><span class="line">docker run -d -p 8080:8080 tomcat  # -d:后台运行 -p:绑定端口 </span><br><span class="line"># 查看容器  </span><br><span class="line">docker ps</span><br><span class="line"># 停止容器  </span><br><span class="line">docker stop [NAME]</span><br><span class="line"># 其他命令</span><br><span class="line">https://www.runoob.com/docker/docker-command-manual.html</span><br></pre></td></tr></table></figure>

<h3 id="使用-Dockerfile-定制镜像"><a href="#使用-Dockerfile-定制镜像" class="headerlink" title="使用 Dockerfile 定制镜像"></a>使用 Dockerfile 定制镜像</h3><h4 id="demo："><a href="#demo：" class="headerlink" title="demo："></a>demo：</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"># 创建Dockerfile</span><br><span class="line">FORM tomcat</span><br><span class="line">WORKDIR /usr/local/tomcat/webapps/ROOT/</span><br><span class="line">RUN &quot;hello Dockerfile&quot; &gt; index.html</span><br><span class="line"></span><br><span class="line"># 构建镜像  </span><br><span class="line">docker build -t mytomcat .</span><br><span class="line"></span><br><span class="line"># 运行  </span><br><span class="line">docker run -p 80:8080 mytomcat bash</span><br></pre></td></tr></table></figure>

<h4 id="镜像构建上下文（Context）"><a href="#镜像构建上下文（Context）" class="headerlink" title="镜像构建上下文（Context）"></a>镜像构建上下文（Context）</h4><blockquote>
<p><a href="https://blog.csdn.net/small_to_large/article/details/77435541" target="_blank" rel="noopener">https://blog.csdn.net/small_to_large/article/details/77435541</a>  </p>
</blockquote>
<h4 id="Dockerfile-指令"><a href="#Dockerfile-指令" class="headerlink" title="Dockerfile 指令"></a>Dockerfile 指令</h4><blockquote>
<p>官方文档：<a href="https://docs.docker.com/engine/reference/builder/" target="_blank" rel="noopener">https://docs.docker.com/engine/reference/builder/</a></p>
</blockquote>
<h3 id="使用数据卷构建MySQL"><a href="#使用数据卷构建MySQL" class="headerlink" title="使用数据卷构建MySQL"></a>使用数据卷构建MySQL</h3><h4 id="什么是数据卷？"><a href="#什么是数据卷？" class="headerlink" title="什么是数据卷？"></a>什么是数据卷？</h4><blockquote>
<p>数据卷 是一个可供一个或多个容器使用的特殊目录，它绕过 <strong>UFS</strong>，可以提供很多有用的特性  </p>
</blockquote>
<ul>
<li>数据卷 可以在<strong>容器之间共享和重用</strong>  </li>
<li>对 数据卷 的<strong>修改会立马生效</strong>  </li>
<li>对 数据卷 的<strong>更新，不会影响镜像</strong>  </li>
<li>数据卷 默认会<strong>一直存在</strong>，即使容器被删除 <strong>（MySQL可以用它来实现持久化）</strong></li>
</ul>
<h4 id="构建MySQL"><a href="#构建MySQL" class="headerlink" title="构建MySQL"></a>构建MySQL</h4><blockquote>
<p>将本地文件加挂载到MySQL容器上，实现MySQL的持久化</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"># 拉取镜像  </span><br><span class="line">docker pull mysql:5.7.28</span><br><span class="line"></span><br><span class="line"># 使用数据卷启动MySQL  </span><br><span class="line">docker run -p 3306:3306 --name mysql \</span><br><span class="line">-v /usr/local/docker/mysql/logs:/var/log/mysql \</span><br><span class="line">-v /usr/local/docker/mysql/data:/var/lib/mysql \</span><br><span class="line">-e MYSQL_ROOT_PASSWORD=123456 \</span><br><span class="line">-d mysql:5.7.28</span><br><span class="line"></span><br><span class="line">## 复制docker容器内容到宿主机  </span><br><span class="line">docker cp mysql:/etc/mysql .  # 可以将MySQL配置负责到本地</span><br></pre></td></tr></table></figure>

<h3 id="Docker-Compose"><a href="#Docker-Compose" class="headerlink" title="Docker Compose"></a>Docker Compose</h3><h4 id="什么是-Docker-Compose"><a href="#什么是-Docker-Compose" class="headerlink" title="什么是 Docker Compose"></a>什么是 Docker Compose</h4><blockquote>
<p>Compose 项目是 Docker 官方的开源项目，负责实现<strong>对 Docker 容器集群的快速编排。</strong><br>Docker Compose 它允许用户通过一个单独的 <strong>docker-compose.yml</strong> 模板文件（YAML 格式）来定义一组相关联的应用容器为一个项目（project）。</p>
</blockquote>
<h4 id="下载、安装"><a href="#下载、安装" class="headerlink" title="下载、安装"></a>下载、安装</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"># GitHub地址：https://github.com/docker/compose</span><br><span class="line"></span><br><span class="line"># 下载，并安装</span><br><span class="line">curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-`uname -s`-`uname -m` &gt; /usr/local/bin/docker-compose</span><br><span class="line">chmod +x /usr/local/bin/docker-compose</span><br><span class="line"></span><br><span class="line"># 验证</span><br><span class="line">docker-compose version</span><br><span class="line"></span><br><span class="line"># 卸载，直接删除二进制文件即可  </span><br><span class="line">rm /usr/local/bin/docker-compose</span><br></pre></td></tr></table></figure>

<h4 id="DEMO"><a href="#DEMO" class="headerlink" title="DEMO"></a>DEMO</h4><blockquote>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"># 新建文件docker-compose.yml</span><br><span class="line"></span><br><span class="line">### 配置tomcat、MySQL</span><br><span class="line">version: &apos;3&apos;</span><br><span class="line">services:</span><br><span class="line">  tomcat:</span><br><span class="line">    restart: always</span><br><span class="line">    image: tomcat</span><br><span class="line">    container_name: tomcat</span><br><span class="line">    ports:</span><br><span class="line">      - 8080:8080</span><br><span class="line">    volumes:</span><br><span class="line">      - /usr/local/docker/tomcat/ROOT:/usr/local/tomcat/webapps/ROOT</span><br><span class="line">  mysql:</span><br><span class="line">    restart: always</span><br><span class="line">    image: mysql:5.7.22</span><br><span class="line">    container_name: mysql</span><br><span class="line">    ports:</span><br><span class="line">      - 3306:3306</span><br><span class="line">    environment:</span><br><span class="line">      TZ: Asia/Shanghai</span><br><span class="line">      MYSQL_ROOT_PASSWORD: 123456</span><br><span class="line">    command:</span><br><span class="line">      --character-set-server=utf8mb4</span><br><span class="line">      --collation-server=utf8mb4_general_ci</span><br><span class="line">      --explicit_defaults_for_timestamp=true</span><br><span class="line">      --lower_case_table_names=1</span><br><span class="line">      --max_allowed_packet=128M</span><br><span class="line">      --sql-mode=&quot;STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO&quot;</span><br><span class="line">    volumes:</span><br><span class="line">      - mysql-data:/var/lib/mysql</span><br><span class="line"></span><br><span class="line">volumes:</span><br><span class="line">  mysql-data:</span><br><span class="line">###</span><br><span class="line"></span><br><span class="line"># 启动</span><br><span class="line">docker-compose up -d</span><br></pre></td></tr></table></figure>

<h3 id="Docker-常用命令"><a href="#Docker-常用命令" class="headerlink" title="Docker 常用命令"></a>Docker 常用命令</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"># 查看 Docker 版本</span><br><span class="line">docker version</span><br><span class="line"></span><br><span class="line"># 从 Dockerfile 文件构建镜像</span><br><span class="line">docker build -t image-name .</span><br><span class="line"></span><br><span class="line"># 运行 Docker 镜像</span><br><span class="line">docker run -d image-name</span><br><span class="line"></span><br><span class="line"># 查看Docker镜像</span><br><span class="line">docker images</span><br><span class="line"></span><br><span class="line"># 查看运行的容器</span><br><span class="line">docker ps</span><br><span class="line">docker ps -al # 查看所有容器  </span><br><span class="line"></span><br><span class="line"># 停止容器  </span><br><span class="line">docker stop conainer_id</span><br><span class="line"></span><br><span class="line"># 删除一个镜像  </span><br><span class="line">docker rmi iamge-name</span><br><span class="line"></span><br><span class="line"># 删除所有镜像</span><br><span class="line">docker rmi $(docker images -q)</span><br><span class="line"></span><br><span class="line"># 强制删除所有镜像</span><br><span class="line">docker rmi -r $(docker images -a)</span><br><span class="line"></span><br><span class="line"># 删除所有虚悬镜像</span><br><span class="line">docker rmi $(docker images -q -f dangling=true)</span><br><span class="line"></span><br><span class="line"># 删除所有容器</span><br><span class="line">docker rm $(docker ps -a -q)</span><br><span class="line"></span><br><span class="line"># 进入 Docker 容器</span><br><span class="line">docker exec -it container-id /bin/bash</span><br><span class="line"></span><br><span class="line"># 查看数据卷  </span><br><span class="line">docker volume ls</span><br><span class="line"></span><br><span class="line"># 删除指定数据卷</span><br><span class="line">docker volume rm [volume_name]</span><br><span class="line"></span><br><span class="line"># 删除所有未关联的数据卷</span><br><span class="line">docker volume rm $(docker volume ls -qf dangling=true)</span><br><span class="line"></span><br><span class="line"># 从主机复制文件到容器</span><br><span class="line">sudo docker cp host_path containerID:container_path</span><br><span class="line"></span><br><span class="line"># 从容器复制文件到主机</span><br><span class="line">sudo docker cp containerID:container_path host_path</span><br></pre></td></tr></table></figure>


        
            <div class="donate-container">
    <div class="donate-button">
        <button id="donate-button">赞赏</button>
    </div>
    <div class="donate-img-container hide" id="donate-img-container">
        <img id="donate-img" src="" data-src="/img/donate.png">
        <p> 感谢鼓励 </p>
    </div>
</div>
        
        <br />
        <div id="comment-container">
        </div>
        <div id="disqus_thread"></div>

        <div id="lv-container">
        </div>

    </div>
</div>

    </div>
</div>


<footer class="footer">
    <ul class="list-inline text-center">
        
        

        

        

        
        <li>
            <a target="_blank"  href="https://github.com/Tooi6">
                            <span class="fa-stack fa-lg">
                                <i class="iconfont icon-github"></i>
                            </span>
            </a>
        </li>
        

        

    </ul>
    
    <p>
        <span>/</span>
        
        <span><a href="https://github.com/Tooi6" target="_blank" rel="noopener">Tooi6</a></span>
        <span>/</span>
        
        <span><a href="#">Tooi-Blog</a></span>
        <span>/</span>
        
        <span><a href="#">It helps SEO</a></span>
        <span>/</span>
        
    </p>
    
    <p>
        <span id="busuanzi_container_site_pv">
            <span id="busuanzi_value_site_pv"></span>PV
        </span>
        <span id="busuanzi_container_site_uv">
            <span id="busuanzi_value_site_uv"></span>UV
        </span>
        Created By <a href="https://hexo.io/" target="_blank" rel="noopener">Hexo</a>  Theme <a href="https://github.com/aircloud/hexo-theme-aircloud" target="_blank" rel="noopener">AirCloud</a></p>
</footer>




</body>

<script>
    // We expose some of the variables needed by the front end
    window.hexo_search_path = "search.json"
    window.hexo_root = "/"
    window.isPost = true
</script>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="/js/index.js"></script>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




</html>
